package interfacesBeans;

/**
 *
 * @author Rodrigo
 */

import agenda.*;
import excecoes.DataInvalida;
import excecoes.FormaDeContatoInvalida;
import excecoes.NomeInvalido;
import javax.swing.JOptionPane;

public class EditarContato extends javax.swing.JPanel {

    private String textLabelErro;
    private int posicaoContato;

    /** Creates new form EditarContato */
    public EditarContato() {
        initComponents();
    }

    public void setInitialFieldValues(int posicaoContato) {

        this.posicaoContato = posicaoContato;
        textLabelErro = "";

        Contato c = Body.user.getAgenda().getContatos().get(posicaoContato);

        textFieldNome.setText(c.getNome());
        textFieldEndereco.setText(c.getEndereco());
        textFieldDataAniversario.setText(c.getDataAniversarioFormatada());
        labelTipoFormaDeContato.setText(c.getFormasDeContato().get(0).getTipo());
        textFieldEnderecoContato.setText(c.getFormasDeContato().get(0).getEndereco());
        textFieldComentario.setText(c.getFormasDeContato().get(0).getComentario());
        labelErro.setText(textLabelErro);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        labelEditarContato = new javax.swing.JLabel();
        labelNome = new javax.swing.JLabel();
        textFieldNome = new javax.swing.JTextField();
        labelEndereco = new javax.swing.JLabel();
        textFieldEndereco = new javax.swing.JTextField();
        labelDataAniversario = new javax.swing.JLabel();
        textFieldDataAniversario = new javax.swing.JTextField();
        labelFormadecontato = new javax.swing.JLabel();
        labelTipoFormaDeContato = new javax.swing.JLabel();
        textFieldEnderecoContato = new javax.swing.JTextField();
        labelComentario = new javax.swing.JLabel();
        textFieldComentario = new javax.swing.JTextField();
        buttonSalvar = new javax.swing.JButton();
        buttonCancelar = new javax.swing.JButton();
        labelErro = new javax.swing.JLabel();

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        labelEditarContato.setFont(new java.awt.Font("Tahoma", 1, 28)); // NOI18N
        labelEditarContato.setText("Editar Contato");

        labelNome.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        labelNome.setText("Nome");

        textFieldNome.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        textFieldNome.setText("O nome do contato a ser editado");

        labelEndereco.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        labelEndereco.setText("Endereço");

        textFieldEndereco.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        textFieldEndereco.setText("O endereco do contato a ser editado");

        labelDataAniversario.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        labelDataAniversario.setText("Data de aniversário");

        textFieldDataAniversario.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        textFieldDataAniversario.setText("dd/MM");

        labelFormadecontato.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        labelFormadecontato.setText("Forma de contato:");

        labelTipoFormaDeContato.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        labelTipoFormaDeContato.setText("Telefone");

        textFieldEnderecoContato.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        textFieldEnderecoContato.setText("O endereco da forma de contato");

        labelComentario.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        labelComentario.setText("Comentário");

        textFieldComentario.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        textFieldComentario.setText("O comentario da forma de contato");

        buttonSalvar.setText("Salvar");
        buttonSalvar.setPreferredSize(new java.awt.Dimension(80, 23));
        buttonSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSalvarActionPerformed(evt);
            }
        });

        buttonCancelar.setText("Cancelar");
        buttonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCancelarActionPerformed(evt);
            }
        });

        labelErro.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        labelErro.setForeground(new java.awt.Color(255, 0, 0));
        labelErro.setText("Mensagens de erro");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelEditarContato)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(labelEndereco)
                            .addComponent(labelNome)
                            .addComponent(labelDataAniversario)
                            .addComponent(labelFormadecontato))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(textFieldNome, javax.swing.GroupLayout.DEFAULT_SIZE, 502, Short.MAX_VALUE)
                            .addComponent(textFieldEndereco, javax.swing.GroupLayout.DEFAULT_SIZE, 502, Short.MAX_VALUE)
                            .addComponent(textFieldDataAniversario, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(labelTipoFormaDeContato)
                                    .addComponent(labelComentario))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(textFieldEnderecoContato, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(textFieldComentario, javax.swing.GroupLayout.DEFAULT_SIZE, 413, Short.MAX_VALUE))))))
                .addContainerGap(142, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(284, 284, 284)
                .addComponent(buttonSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33)
                .addComponent(buttonCancelar)
                .addContainerGap(305, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(79, 79, 79)
                .addComponent(labelErro, javax.swing.GroupLayout.PREFERRED_SIZE, 531, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(190, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {buttonCancelar, buttonSalvar});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(labelEditarContato)
                .addGap(48, 48, 48)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelNome)
                    .addComponent(textFieldNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textFieldEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelEndereco))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelDataAniversario)
                    .addComponent(textFieldDataAniversario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(63, 63, 63)
                        .addComponent(labelFormadecontato))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(46, 46, 46)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(labelTipoFormaDeContato)
                            .addComponent(textFieldEnderecoContato, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(labelComentario)
                            .addComponent(textFieldComentario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(39, 39, 39)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonCancelar))
                .addGap(47, 47, 47)
                .addComponent(labelErro, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(44, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {buttonCancelar, buttonSalvar});

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

        private void buttonSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSalvarActionPerformed

        textLabelErro = "";

        Contato c = Body.user.getAgenda().getContatos().get(posicaoContato);

        String nome = textFieldNome.getText();
        String endereco = textFieldEndereco.getText();
        String dataFormatada = textFieldDataAniversario.getText();

        String enderecoContato = textFieldEnderecoContato.getText();
        String comentario = textFieldComentario.getText();
        String tipoFormaDeContato = c.getFormasDeContato().get(0).getTipo();
        FormaDeContato f;

        try {
            if (tipoFormaDeContato == "E-mail") {
                f = new Email(enderecoContato, comentario);
            } else if (tipoFormaDeContato == "Skype") {
                f = new Skype(enderecoContato, comentario);
            } else {
                f = new Telefone(enderecoContato, comentario);
            }

            c.getFormasDeContato().set(0, f);
            c.setNome(nome);
            c.setEndereco(endereco);
            c.setDataAniversario(dataFormatada);

            JOptionPane.showMessageDialog(this, "As alterações foram salvas!", "Contato alterado", JOptionPane.INFORMATION_MESSAGE);

            java.awt.CardLayout layout = (java.awt.CardLayout) Body.panelBody.getLayout();
            layout.show(Body.panelBody, "Contatos");

        } catch (FormaDeContatoInvalida fi) {
            textLabelErro = fi.getMessage();
        } catch (NomeInvalido n) {
            textLabelErro = n.getMessage();
        } catch (DataInvalida d) {
            textLabelErro = d.getMessage();
        }

        labelErro.setText(textLabelErro);
    }//GEN-LAST:event_buttonSalvarActionPerformed

    private void buttonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelarActionPerformed
        java.awt.CardLayout layout = (java.awt.CardLayout)Body.panelBody.getLayout();
        layout.show(Body.panelBody, "Contatos");
    }//GEN-LAST:event_buttonCancelarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCancelar;
    private javax.swing.JButton buttonSalvar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel labelComentario;
    private javax.swing.JLabel labelDataAniversario;
    private javax.swing.JLabel labelEditarContato;
    private javax.swing.JLabel labelEndereco;
    private javax.swing.JLabel labelErro;
    private javax.swing.JLabel labelFormadecontato;
    private javax.swing.JLabel labelNome;
    private javax.swing.JLabel labelTipoFormaDeContato;
    private javax.swing.JTextField textFieldComentario;
    private javax.swing.JTextField textFieldDataAniversario;
    private javax.swing.JTextField textFieldEndereco;
    private javax.swing.JTextField textFieldEnderecoContato;
    private javax.swing.JTextField textFieldNome;
    // End of variables declaration//GEN-END:variables
}
